<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/13
 * Time: 22:07
 */
namespace app\model;


use think\facade\Db;
use think\Model;

class CleaningCheck extends Model
{

    private static function _getListOutPutFields()
    {
        return [
            'ck_id as inspectionId',
            'ck_finish as finish',
            'cleaning_time as cleaningTime',
            'cleaning_name as pername',
            'cp_name as cleaningPointName'
        ];
    }
    public function addOneCheck($data)
    {
        $data['ck_create_time'] = time();
        return Db::table('cleaning_check')->insertGetId($data);
    }

    public function getInfoByWhere($where)
    {
        return Db::table('cleaning_check')->where($where)->find();
    }

    public function getCheckAllCountByWhere($where)
    {
        return Db::table('cleaning_check')->where($where)->count();
    }

    public function getCheckListByWhere($where,$field = '',$group = 'ck_id')
    {
        $field = $field ? $field : self::_getListOutPutFields();
        return Db::table('cleaning_check')
            ->leftJoin('cleaning','cleaning_id=ck_cleaning_id')
            ->leftJoin('cleaning_point','cp_id=ck_cleaning_point_id')
            ->where($where)
            ->field($field)
            ->group($group)
            ->select();
    }

    public function getCheckMonthListByWhere($where,$monthWhere)
    {
        $sql = "SELECT
	datelist as dday,
	IFNULL(t1.cnt,0) as cnt
FROM
	calendar
	LEFT JOIN (
	SELECT 
		FROM_UNIXTIME( ck_create_time, '%Y-%m-%d' ) AS dday,
		count( ck_id ) AS cnt 
	FROM
		`cleaning_check` 
	WHERE
		{$where}
	GROUP BY
	dday 
	) t1 on calendar.datelist=t1.dday
	where {$monthWhere} GROUP BY datelist";

        return Db::query($sql);

    }

}